import play.GlobalSettings;
import play.libs.F;
import play.mvc.Http;
import play.mvc.Http.RequestHeader;
import play.mvc.Results;
import play.mvc.SimpleResult;
import util.BrowserType;
import util.HttpUtils;

import java.util.Map;

/**
 * Created by Bill Lyu on 5/19/2014.
 */
public class Global extends GlobalSettings {
    @Override
    public F.Promise<SimpleResult> onHandlerNotFound(RequestHeader request) {
        BrowserType browserType = getBrowserType();
        F.Promise<SimpleResult> pure;
        switch (browserType) {
            case mobile:
                pure = F.Promise.<SimpleResult>pure(
                        Results.notFound(views.html.mobile.error404.render("Your new application is ready.")));
                break;
            case smartphone:
                pure = F.Promise.<SimpleResult>pure(
                        Results.notFound(views.html.smartphone.error404.render("Your new application is ready.")));
                break;
            default:
                pure = F.Promise.<SimpleResult>pure(
                        Results.notFound(views.html.web.error404.render("Your new application is ready.")));
                Http.Session session = Http.Context.current().session();
                session.put("Browser_d", "web");
                break;
        }
        return pure;
    }

    private static BrowserType getBrowserType() {
        Http.Request req = Http.Context.current().request();
        Map<String, String[]> headerMap = req.headers();
        return HttpUtils.getBrowserType(headerMap);
    }
}